package xyz.ylx.apirotation.config;

import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

@Setter
@Slf4j
@ConfigurationProperties(prefix = "gemini.api")
public class GeminiApiKeyConfig {
    
    private List<String> keys;
    private String url;
    private String openaiUrl;
    
    public List<String> getKeys() {
        // 优先从环境变量获取API密钥
        String envKeys = System.getenv("GEMINI_API_KEYS");
        if (StringUtils.hasLength(envKeys)) {
            log.info("从环境变量读取Gemini API密钥");
            return Arrays.asList(envKeys.split(","));
        }
        return keys;
    }

    public String getUrl() {
        // 优先从环境变量获取URL
        String envUrl = System.getenv("GEMINI_API_URL");
        if (StringUtils.hasLength(envUrl)) {
            return envUrl;
        }
        return url;
    }

    public String getOpenaiUrl() {
        // 优先从环境变量获取OpenAI兼容URL
        String envOpenaiUrl = System.getenv("GEMINI_OPENAI_URL");
        if (StringUtils.hasLength(envOpenaiUrl)) {
            return envOpenaiUrl;
        }
        return openaiUrl;
    }

}